namespace Yaaf.WP7.NativeAccess.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    public class DirectoryInfo : FileSystemEntry
    {
        public override bool Exists
        {
            get
            {
                return Directory.Exists(this.FullName);
            }
        }

        public DirectoryInfo(string fullName)
            : base(fullName == "" ? "\\" : fullName)
        {
        }

        public override void Delete()
        {
            this.Delete(false);
        }

        public override FileSystemEntry Move(DirectoryInfo targetDir, string newName = null, bool overwrite = false)
        {
            string newPath = Path.Combine(targetDir.FullName, string.IsNullOrEmpty(newName) ? this.Name : newName);
            Directory.CopyTo(this.FullName, targetDir.FullName, newName, overwrite);
            Directory.Delete(this.FullName, true);
            return new DirectoryInfo(newPath);
            //var newPath = Path.Combine(targetDir.FullName, string.IsNullOrEmpty(newName) ? this.Name : newName);
            //Directory.Move(this.FullName, newPath, overwrite);
            //return new FileInfo(newName);
        }

        public override FileSystemEntry CopyTo(DirectoryInfo targetDir, string newName = null, bool overwrite = false)
        {
            string newPath = Path.Combine(targetDir.FullName, string.IsNullOrEmpty(newName) ? this.Name : newName);
            Directory.CopyTo(this.FullName, targetDir.FullName, newName, overwrite);
            //File.Copy(this.FullName, newPath, overwrite);
            return new DirectoryInfo(newPath);
        }

        public void Delete(bool recursive = false)
        {
            Directory.Delete(this.FullName, recursive);
        }

        public IEnumerable<DirectoryInfo> Directories
        {
            get
            {
                return Directory.GetDirectories(this.FullName).Select(s => new DirectoryInfo(s));
            }
        }

        public IEnumerable<FileInfo> Files
        {
            get
            {
                return Directory.GetFiles(this.FullName).Select(s => new FileInfo(s));
            }
        }

        public IEnumerable<FileSystemEntry> Entries
        {
            get
            {
                return Directory.GetFileSystemEntries(this.FullName).Select(FileSystemEntry.GetFileSystemEntry);
            }
        }

        public void Create()
        {
            if (Exists)
            {
                return;
            }

            Directory.Create(this.FullName);
        }

        public IEnumerable<FileSystemEntry> FilterEntries(string currentFilter)
        {
            return
                Directory.GetFileSystemEntries(this.FullName, currentFilter).Select(FileSystemEntry.GetFileSystemEntry);
        }

        public IEnumerable<FileSystemEntry> FilterFiles(string currentFilter)
        {
            return
                Directory.GetFiles(this.FullName, currentFilter).Select(s => (FileSystemEntry)new FileInfo(s));
        }

        public IEnumerable<FileSystemEntry> FilterDirectories(string currentFilter)
        {
            return Directory.GetDirectories(this.FullName, currentFilter).Select(s => (FileSystemEntry)new DirectoryInfo(s));
        }
    }
}